/**
 *  Copyright 2009 Henning Teek, Ingo Renner
 *  
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *  
 *  http://www.apache.org/licenses/LICENSE-2.0
 *  
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License. 
 */

package com.googlecode.bricket.web.plugin;

import java.util.Arrays;
import java.util.Collection;
import java.util.List;

import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;

import brix.Brix;
import brix.Plugin;
import brix.jcr.api.JcrSession;
import brix.web.tab.AbstractWorkspaceTab;
import brix.web.tab.IBrixTab;
import brix.workspace.Workspace;

import com.googlecode.bricket.plugin.mail.web.MailPlugin;

/**
 * @author Ingo Renner
 * @author Henning Teek
 */
public class ServicesPlugin implements Plugin {
    private static final String ID = ServicesPlugin.class.getName();
    
    private Brix brix;

    public static ServicesPlugin get(Brix brix) {
        return (ServicesPlugin)brix.getPlugin(ID);
    }

    public static ServicesPlugin get() {
        return get(Brix.get());
    }
    
    public ServicesPlugin(Brix brix) {
    	this.brix = brix;
    	registerServicePlugin(new MailPlugin());
    }
    
    public Collection<ServicePlugin> getServicePlugins() {
        return brix.getConfig().getRegistry().lookupCollection(ServicePlugin.POINT);
    }
    
    public ServicePlugin getServicePluginForType(String type) {
        for (ServicePlugin plugin : getServicePlugins()) {
            if (plugin.getServiceType().equals(type)) {
                return plugin;
            }
        }
        throw new IllegalArgumentException("Argument 'id' cannot be " + type);
    }
    
    public final Brix getBrix() {
        return brix;
    }

    public void registerServicePlugin(ServicePlugin plugin) {
        if (plugin == null) {
            throw new IllegalArgumentException("Argument 'plugin' cannot be null");
        }
        brix.getConfig().getRegistry().register(ServicePlugin.POINT, plugin);
    }
    
    @Override
	public String getId() {
		return getClass().getName();
	}

	@Override
	public String getUserVisibleName(Workspace workspace, boolean isFrontend) {
		return null;
	}

	@Override
	public List<Workspace> getWorkspaces(Workspace currentWorkspace, boolean isFrontend) {
		return null;
	}

	@Override
	public void initWorkspace(Workspace workspace, JcrSession workspaceSession) {
	}

	@Override
	public boolean isPluginWorkspace(Workspace workspace) {
		return false;
	}

	@Override
	public List<IBrixTab> newTabs(IModel<Workspace> workspaceModel) {
        IBrixTab tabs[] = new IBrixTab[]{new Tab(new Model<String>("Services"), workspaceModel)};
        return Arrays.asList(tabs);
	}

    static class Tab extends AbstractWorkspaceTab {
        public Tab(IModel<String> title, IModel<Workspace> workspaceModel) {
            super(title, workspaceModel, 98);
        }

        @Override
        public Panel newPanel(String panelId, IModel<Workspace> workspaceModel) {
            return new ServicesPanel(panelId, workspaceModel);
        }
    }	
}
